var app = angular.module('meetingApp.controllers');

app.controller('settingCtr', ['$rootScope', '$scope', 'ajaxService', 'toastPCService'
    , function ($rootScope, $scope, ajax, toast) {

        $scope.data = {
            tabList: [
                {name: '基础设置', key: 'base'}
            ],
            index: 0,
            edit: {},
            loading: true,
            uploading: false
        };
        $scope.changeTab = function (index) {
            $scope.data.index = index;
        }

        $scope.getSetting = function () {
            ajax('getSetting', {}, function (rel) {
                if (rel.data) {
                    $scope.data.edit = rel.data
                    $scope.data.loading = false
                    console.log($scope.data.edit)
                }
            })
        }

        $scope.commit = function () {
            if ($('#editForm').valid()) {
                var content = $scope.data.edit[$scope.data.tabList[$scope.data.index].key];
                if ($scope.data.tabList[$scope.data.index].key == 'base') {
                    console.log(content)
                    if (content.min_nianbao >= content.max_nianbao) return toast.show(3, '年报最小金额不得大于等于最大金额');
                    if (content.min_zhuxiao >= content.max_zhuxiao) return toast.show(3, '注销最小金额不得大于等于最大金额');
                }
                ajax('saveSetting', {key: $scope.data.tabList[$scope.data.index].key, content: content}, function (rel) {
                    if (!rel.errCode) {
                        toast.show('保存成功')
                    }
                })
            }
        }

        $scope.$on('$stateChangeSuccess', function () {
            $scope.getSetting();
        })


    }])